#include "MyString.h"

std::istream& operator >> (std::istream& in,MyString& _string)
{
	std::string s;
	in >> s;
	_string.ConvertFromString(s);
	return in;
}

std::ostream& operator << (std::ostream& os,MyString& _string)
{
	if(_string.data != NULL)
	{
		os << _string.data;
	}
	return os;
}

MyString::MyString(void)
{
	size = 0;
	data = NULL;
}


MyString::~MyString(void)
{
	if(data != NULL)
	{
		delete []data;
		data = NULL;
	}
}


MyString::MyString(const char* c)
{
	size = strlen(c) + 1;
	data = new char[size];
	if(data != NULL)
	{
		memcpy(data,c,size);
	}
}
MyString::MyString(const MyString& _string)
{
	size = _string.size;
	data = new char[size];
	if(data != NULL)
	{
		memcpy(data,_string.data,size);
	}
}

MyString::MyString( std::string s )
{
	size = s.length() + 1;
	data = new char[size];
	const char* c = s.c_str();
	if(data != NULL)
	{
		memcpy(data,c,size);
	}
}


MyString& MyString::operator = (const MyString& _string)
{
	size = _string.size;
	data = new char[size];
	if(data != NULL)
	{
		memcpy(data,_string.data,size);
	}
	return *this;
}

MyString& MyString::ConvertFromString( const std::string s )
{
	this->size = s.length() + 1;
	this->data = new char[size];
	const char* c = s.c_str();
	if(data != NULL)
	{
		memcpy(data,c,size);
	}
	return *this;
}

MyString::operator char*() const
{
	return data;
}
